package com.game.fighting.socket;

import com.almasb.fxgl.dsl.FXGL;

import java.util.Timer;

/**
 * Copyright (C), 2021-2041, chenc 豫R7HQ53
 *
 * @author: chenc
 * Date:     2023/12/8 21:19
 * Description:
 */
public class NetService {

    /**
     * 创建网络服务和定时任务
     * @param url
     * @throws Exception
     */
    public static SocketClient getNetClient(String url) throws Exception {
        SocketClient socketClient = SocketUtil.createSocket(url, 2000L);
        Timer heartTimer = SocketUtil.heartTimer(socketClient, 5000L);
        FXGL.getPrimaryStage().setOnCloseRequest(event -> {
            heartTimer.cancel();
            socketClient.close();
            System.out.println("连接关闭");
        });
        return socketClient;
    }
}
